﻿@model IEnumerable<Citi.HeadCount.WebUI.Models.GOCAttributesViewModel>
@{
    ViewBag.Title = "ShowReport";
}
@if (TempData["Exception"] != null && (bool)TempData["Exception"])
{
    <script type="text/javascript">
        $(function () {
            $("#Value").attr('readonly', true);
            $("#Type").val('-');
            $("#Value").val('');
            $("#Level").val('');

            $("#Type").bind('change', function () {
                var selected = $("#Type :selected").val();
                var value = $("#Value");
                value.val('');

                if (selected == '@((int)Citi.HeadCount.Domain.RuleType.LVID)') {
                    value.val($("#LVID" + $("#SelectedId").val()).html());
                } else if (selected == '@((int)Citi.HeadCount.Domain.RuleType.OU)') {
                    value.val($("#FRSOU" + $("#SelectedId").val()).html());
                }
            });

            $("#Level").blur(function () {
                var selected = $("#Type :selected").val();
                var value = $("#Value");
                value.val('');

                if (selected == '@((int)Citi.HeadCount.Domain.RuleType.ManagedSegment)') {
                    value.val($("#ManagedSegmentLevel" + $("#Level").val() + "-" + $("#SelectedId").val()).html());
                } else if (selected == '@((int)Citi.HeadCount.Domain.RuleType.Function)') {
                    value.val($("#FunctionLevel" + $("#Level").val() + "-" + $("#SelectedId").val()).html());
                }
            });

            $("#divApplyRule").dialog({
                autoOpen: false,
                modal: true,
                width: 720,
                closeOnEscape: false,
                resizable: false,
                buttons:
                {
                    Ok: function () {
                        if ($(".hiddenRuleResultId").val() > 0) {
                            var ruleResultId = $(".hiddenRuleResultId").val();
                            var gocId = $("#SelectedId").val();
                            
                            $.ajax({
                                url: '@Url.Action("SetRuleResult", "GOC")',
                                type: 'POST',
                                assync: false,
                                data: { id: gocId, ruleResultId: ruleResultId, applyToAll: $("#applyToAll").val() },
                                success: function () {
                                    alert('Exceção criada com sucesso', '@Url.Action("ShowGOCsException", "GOC")');
                                    $("#applyRule" + gocId).hide();
                                    $("#divApplyRule").dialog('close');
                                },
                                error: function () {
                                    alert("Error");
                                }
                            });
                        } else {
                            alert("There is no rule associated to this exception.");
                        }
                    },
                    Cancel: function () {
                        if ($(".hiddenRuleResultId").val() > 0) {
                            $.ajax({
                                url: '@Url.Action("Inative", "RuleResult")',
                                type: 'POST',
                                data: { id: $(".hiddenRuleResultId").val() }
                            });

                            window.location = '@Url.Action("ShowGOCsException", "GOC")';
                        }

                        $("#divApplyRule").dialog('close');
                    }
                }
            });
        });

        function openExceptionDialog(id) {
            $("#SelectedId").val(id);
            $("#divApplyRule").dialog("open");
        }
    </script>

    <div title="Apply rule" id="divApplyRule">
        @Html.Action("CreateException", "RuleResult")
        <br />
        @Html.CheckBox("applyToAll") Apply To All
    </div>
    
    @Html.Hidden("SelectedId")
}
<table id="reportTable">
    <tr>
        <th>
        </th>
        <th style="width: 290px !important;">
            GOC Attribute
        </th>
        <th>
            GOC (asNumber)
        </th>
        <th>
            Long Description
        </th>
        <th>
            Corp Code
        </th>
        <th>
            Expense Code
        </th>
        <th>
            Status
        </th>
        <th>
            Updated Date
        </th>
        <th>
            SID
        </th>
        <th>
            Local Cost Code
        </th>
        <th>
            Manager Id
        </th>
        <th>
            FRSBU
        </th>
        <th>
            FRSBU Long Description
        </th>
        <th>
            FRSOU
        </th>
        <th>
            FRSOU Long Description
        </th>
        <th>
            Function
        </th>
        <th>
            Function Short Description
        </th>
        <th>
            LVID
        </th>
        <th>
            LVID Long Description
        </th>
        <th>
            Managed Geography
        </th>
        <th>
            Managed Geography Description
        </th>
        <th>
            Managed SegmentId
        </th>
        <th>
            Managed Segment Long Description
        </th>
        @{
            var functionsCount = Model.Max(x => x.FunctionsLevels.Count());
            var managedSegmentCount = Model.Max(x => x.ManagedSegmentLevels.Count());

            for (int i = 0; i < managedSegmentCount; i++)
            {
            <th>
                Managed Segment L@(i + 1)
            </th>
            }

            for (int i = 0; i < functionsCount; i++)
            {
            <th>
                Function L@(i + 1)
            </th>
            }
        }
    </tr>
    @foreach (var item in Model)
    {
        <tr id="@string.Concat("tableRow", item.Id)">
            <td>
                @if (TempData["Exception"] != null && (bool)TempData["Exception"])
                {
                    <a href="#" onclick="openExceptionDialog('@item.Id')" id="@string.Concat("applyRule", item.Id)">
                        Apply Rule</a>
                }
            </td>
            <td>
                @item.GOCAttribute
            </td>
            <td>
                @item.GOCAsNumber
            </td>
            <td>
                @item.LongDescription
            </td>
            <td>
                @item.CorpCode
            </td>
            <td>
                @item.ExpenseCode
            </td>
            <td>
                @item.Status
            </td>
            <td>
                @item.UpdatedDate
            </td>
            <td>
                @item.SID
            </td>
            <td>
                @item.LocalCostCode
            </td>
            <td>
                @item.ManagerId
            </td>
            <td>
                @item.FRSBU
            </td>
            <td>
                @item.FRSBULongDescription
            </td>
            <td>
                <span id="@string.Concat("FRSOU", item.Id)">@item.FRSOU</span>
            </td>
            <td>
                @item.FRSOULongDescription
            </td>
            <td>
                @item.Function
            </td>
            <td>
                @item.FunctionShortDescription
            </td>
            <td>
                <span id="@string.Concat("LVID", item.Id)">@item.LVID</span>
            </td>
            <td>
                @item.LVIDLongDescription
            </td>
            <td>
                @item.ManagedGeography
            </td>
            <td>
                @item.ManagedGeographyDescription
            </td>
            <td>
                @item.ManagedSegmentId
            </td>
            <td>
                @item.ManagedSegmentLongDescription
            </td>
            @*@foreach (var level in item.ManagedSegmentLevels)
            {
                <td>@level
                </td>
            }*@
            @for (int i = 0; i < managedSegmentCount; i++)
            {
                <td>
                    @if (i < item.ManagedSegmentLevels.Count())
                    {
                        <span id="@string.Concat("ManagedSegmentLevel", i + 1, "-", item.Id)">@item.ManagedSegmentLevels[i]</span>
                    }
                    else
                    {
                        <text>-</text>
                    }
                </td>
            }
            @*            @foreach (var level in item.FunctionsLevels)
            {
                <td>@level
                </td>
            }*@
            @for (int i = 0; i < functionsCount; i++)
            {
                <td>
                    @if (i < item.FunctionsLevels.Count())
                    {
                        <span id="@string.Concat("FunctionLevel", i + 1, "-", item.Id)">@item.FunctionsLevels[i]</span>
                    }
                    else
                    {
                        <text>-</text>
                    }
                </td>
            }
        </tr>
    }
</table>
